package kevin.framework.basic.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.lang3.StringUtils;

/**
 * @Description: 参数URLDecode
 * @author Yohann.lu
 * @date 2016年9月21日 下午1:50:21
 * @version v1.0.0
 *
 */
public class ParameterRequestWrapper extends HttpServletRequestWrapper {

	private Map<String, String[]> params = new HashMap<String, String[]>();

	public ParameterRequestWrapper(HttpServletRequest request, Map<String, String[]> newParams) {
		super(request);
		renewParameterMap(request);
	}

	@Override
	public String getParameter(String name) {
		String result = "";
		Object v = params.get(name);
		if (v == null) {
			result = null;
		} else if (v instanceof String[]) {
			String[] strArr = (String[]) v;
			if (strArr.length > 0) {
				result = strArr[0];
			} else {
				result = null;
			}
		} else if (v instanceof String) {
			result = (String) v;
		} else {
			result = v.toString();
		}
		return result;
	}

	@Override
	public Map<String, String[]> getParameterMap() {
		return params;
	}

	@Override
	public Enumeration<String> getParameterNames() {
		return new Vector<String>(params.keySet()).elements();
	}

	@Override
	public String[] getParameterValues(String name) {
		String[] result = null;
		Object v = params.get(name);
		if (v == null) {
			result = null;
		} else if (v instanceof String[]) {
			result = (String[]) v;
		} else if (v instanceof String) {
			result = new String[] { (String) v };
		} else {
			result = new String[] { v.toString() };
		}
		return result;
	}

	private void renewParameterMap(HttpServletRequest request) {
		String method = request.getMethod();
		if (method.equalsIgnoreCase("get")) {// 解决中文乱码问题
			// 取出客户提交的参数集
			Enumeration<String> paramNames = request.getParameterNames();
			// 遍历参数集取出每个参数的名称及值
			while (paramNames.hasMoreElements()) {
				String name = paramNames.nextElement(); // 取出参数名称
				String values[] = request.getParameterValues(name); // 根据参数名称取出其值
				// 如果参数值集不为空
				if (values != null) {
					// 遍历参数值集
					for (int i = 0; i < values.length; i++) {
						try {
							String v = values[i];// URLDecoder.decode(values[i],"utf-8");
							String vlustr = new String(v.getBytes("iso-8859-1"), "UTF-8");
							values[i] = vlustr;
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
					}
					this.params.put(name, values);
				}
			}
		} else {// 对post的参数进行统一urldecode
			Map<String, String[]> parameters = request.getParameterMap();
			String notDecode = request.getParameter("_not_url_decode_");
			Set<String> keys = parameters.keySet();
			for (String key : keys) {
				String[] values = parameters.get(key);
				for (int i = 0; i < values.length; i++) {
					try {
						if(StringUtils.isNotEmpty(notDecode) || "token".equals(key)) {
							values[i] = values[i];
						}else {
							values[i] = URLDecoder.decode(values[i], "utf-8");
						}						
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
				this.params.put(key, values);
			}
		}

	}

}
